package com.eamois.dao.sys.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.sys.CodeSettingDao;
import com.eamois.domain.sys.CodeSetting;
import com.eamois.domain.sys.Device;
import com.eamois.domain.sys.DeviceType;

public class CodeSettingDaoImpl extends BaseDaoImpl<CodeSetting> implements CodeSettingDao{
	@SuppressWarnings("unchecked")
	@Override
	public boolean findCodeSetting(final String code) {
		// TODO Auto-generated method stub
		return (Boolean) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(CodeSetting.class);
				if(code!=null&&!code.equals("")){
					criteria.add(Restrictions.eq("code",code));
				}
				if(criteria.list().size()==0){
					return false;
				}else{
					return true;
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<CodeSetting> findAll() {
		// TODO Auto-generated method stub
		return (Collection<CodeSetting>) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(CodeSetting.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<CodeSetting> find(final String code,final boolean isLike) {
		// TODO Auto-generated method stub
		return (Collection<CodeSetting>) this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				if(isLike){
					Criteria criteria = session.createCriteria(CodeSetting.class);
					if(code!=null&&!code.equals("")){
						criteria.add(Restrictions.like("code","%"+code+"%"));
					}
					return criteria.list();
				}else{
					Criteria criteria = session.createCriteria(CodeSetting.class);
					if(code!=null&&!code.equals("")){
						criteria.add(Restrictions.eq("code",code));
					}
					return criteria.list();
				}
				
			}
		});
	}

}
